/* 
 * Created on 2007-4-22
 * Last modified on 2007-4-22
 * Powered by YeQiangWei.com
 */
package com.yeqiangwei.commons.image;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import org.apache.log4j.Logger;

public abstract class ImageOperate {

	private static final Logger logger = Logger.getLogger(ImageOperate.class);

	public static final int JPEG = 1;

	public static final int GIF = 2;
	
	public static final int JMAGICK = 3;

	/**
	 * 设置成正方形
	 * 
	 * @param opath
	 * @param npath
	 * @return
	 */
	public abstract boolean setSquare(String opath, String npath);
	
	/**
	 * 成比例缩放
	 * @param opath
	 * @param npath
	 * @param width
	 * @param height
	 * @return
	 */
	public abstract boolean scaled(String opath, String npath, int width, int height);
	
	
	public abstract boolean zoomIn(String opath, String npath, int width, int height);
	
	public abstract boolean zoomOut(String opath, String npath, int width, int height);


	public abstract boolean resize(String opath, String npath, int width, int height);

	/**
	 * 剪裁AND缩放图片
	 * 
	 * @param bis
	 * @param filePathAndName
	 * @param w
	 * @param h
	 * @param x
	 * @param y
	 * @param x_w
	 * @param y_h
	 * @return
	 */
	public abstract boolean clip(BufferedInputStream bis, String filePathAndName, int w, int h, int x, int y, int x_w, int y_h);

	public boolean clip(String path, String filePathAndName, int w, int h, int x, int y, int x_w, int y_h) {
		boolean bea = false;
		try {
			FileInputStream fis = new FileInputStream(new File(path));
			BufferedInputStream bis = new BufferedInputStream(fis);
			bea = clip(bis, filePathAndName, w, h, x, y, x_w, y_h);
		} catch (FileNotFoundException e) {
			logger.error(e.toString());
		}
		return bea;
	}

	/**
	 * 按坐标切割图片
	 * @param file
	 * @param newfilepath
	 * @param w 切割后的宽度
	 * @param h 切割后的高度
	 * @param x
	 * @param y
	 * @param x_w 切割时的宽度
	 * @param y_h 切割时的高度
	 * @return
	 */
	public abstract boolean incise(File file, String newfilepath, int w, int h, int x, int y, int x_w, int y_h);

	/**
	 * 按坐标切割图片
	 * @param path
	 * @param newfilepath
	 * @param w 切割后的宽度
	 * @param h 切割后的高度
	 * @param x
	 * @param y
	 * @param x_w 切割时的宽度
	 * @param y_h 切割时的高度
	 * @return
	 */
	public boolean incise(String path, String newfilepath, int w, int h, int x, int y, int x_w, int y_h) {
		return this.incise(new File(path), newfilepath, w, h, x, y, x_w, y_h);
	}
	
	/**
	 * 切割图片坐标 y = 0 , x 自动计算中间部分
	 * @param opath
	 * @param npath
	 * @param width
	 * @param height
	 * @return
	 */
	public abstract boolean inciseTopAndCenter(String opath, String npath, int width, int height);
	
	
	/**
	 * 缩放图片
	 * 
	 * @param w
	 * @param h
	 * @param opath
	 * @param npath
	 * @return
	 */
	public abstract boolean narrowOrEnlarge(String opath, String npath, int w, int h);

	/**
	 * 给图片加水印
	 * 
	 * @param opath
	 *            原图路径
	 * @param npath
	 *            加水印后的新路径
	 * @param wpath
	 *            水印图路径
	 * @param w
	 * @param h
	 * @return
	 */
	public abstract boolean watermark(String opath, String npath, String wpath);

	/**
	 * 
	 * @param opath
	 * @param npath
	 * @param wpath
	 * @param minwidth
	 *            小于此宽度的图片不加水印
	 * @param minheight
	 *            小于此高度的图片不加水印
	 * @return
	 */
	public abstract boolean watermark(String opath, String npath, String wpath, int minwidth, int minheight);
}
